package zozopo.warehouse;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.GoodsclassOS;
import zozopo.warehouse.operations.GoodsclassTB;
import zozopo.warehouse.operations.GoodsclasslistOS;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class PreModifyGoodsAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.GOODS.MODIFY";
	
	//传入参数
	private int id;
	
	
	//传出参数
	private GoodsTB goods;
	
	private List<GoodsclassTB> goodsclassList;
	private LinkedHashMap<Integer,GoodsclassTB> goodsclassMap;
	
	private LinkedHashMap<Integer,Integer> goodsclasslistMap;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		GoodsOS goodsOS = new GoodsOS();
		goods = goodsOS.getGoodsById(id);
		
		
		GoodsclassOS goodsclassOS = new GoodsclassOS(goodsOS.getConn());
		GoodsclasslistOS goodsclasslistOS = new GoodsclasslistOS(goodsOS.getConn());
		
		goodsclassList = goodsclassOS.select();
		
		goodsclassMap = new  LinkedHashMap<Integer,GoodsclassTB>();
		
		getGoodsclassMap(0);
		
		goodsclasslistMap = goodsclasslistOS.getClassNGoodsidMap(id);
		
		goodsOS.releaseConnection();
		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有添加产品权限.");
			return;
		}
	}
	
	public LinkedHashMap<Integer,GoodsclassTB>  getGoodsclassMap(int parentid){
		
		
		for(GoodsclassTB goodsclass:goodsclassList){
			if(!goodsclassMap.containsKey(goodsclass.getId_goodsclass())){//如果已不存在
				if(goodsclass.getParentid_goodsclass() == parentid){//如果父ID正常
					goodsclassMap.put(goodsclass.getId_goodsclass(), goodsclass); 
					getGoodsclassMap(goodsclass.getId_goodsclass());
				}
			}
		}
		
		return goodsclassMap;
	}
	
	
	public GoodsTB getGoods() {
		return goods;
	}
	public void setGoods(GoodsTB goods) {
		this.goods = goods;
	}
	public void setId(int id) {
		this.id = id;
	}
	public static String getLimitedcode() {
		return limitedCode;
	}
	public List<GoodsclassTB> getGoodsclassList() {
		return goodsclassList;
	}
	public void setGoodsclassList(List<GoodsclassTB> goodsclassList) {
		this.goodsclassList = goodsclassList;
	}
	public LinkedHashMap<Integer, GoodsclassTB> getGoodsclassMap() {
		return goodsclassMap;
	}
	public void setGoodsclassMap(LinkedHashMap<Integer, GoodsclassTB> goodsclassMap) {
		this.goodsclassMap = goodsclassMap;
	}
	public int getId() {
		return id;
	}
	public LinkedHashMap<Integer, Integer> getGoodsclasslistMap() {
		return goodsclasslistMap;
	}
	public void setGoodsclasslistMap(
			LinkedHashMap<Integer, Integer> goodsclasslistMap) {
		this.goodsclasslistMap = goodsclasslistMap;
	}
}
